package com.coderush.admin.sys.controller;

import com.coderush.admin.core.common.PageResult;
import com.coderush.admin.core.response.Result;
import com.coderush.admin.sys.model.dto.DepartmentAddDto;
import com.coderush.admin.sys.model.dto.DepartmentUpdateDto;
import com.coderush.admin.sys.model.vo.DepartmentPageVo;
import com.coderush.admin.sys.model.vo.DepartmentVo;
import com.coderush.admin.sys.service.DepartmentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * PackageName com.coderush.admin.sys.controller
 *
 * @Author Eason
 * @Version v1.0
 * @ClassName DepartmentController
 * @Date 2025/8/25
 * @Description TODO
 */
@RestController
@RequestMapping("/sys/dept")
@Validated  // 类级别添加，用于方法参数校验
@Tag(name = "部门管理", description = "部门管理")
@AllArgsConstructor
public class DepartmentController {

    private final DepartmentService departmentService;

    /**
     * 分页查询部门列表
     */
    @Operation(summary = "分页查询部门列表", description = "分页查询部门列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "401", description = "未授权"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/findByPage")
    public Result<PageResult<DepartmentPageVo>> findByPage(String deptName
                                                            ,@RequestParam(defaultValue = "1") Integer pageNumber
                                                            ,@RequestParam(defaultValue = "20") Integer pageSize) {
        var pageResult = departmentService.findByPage(deptName, pageNumber, pageSize);
        return Result.buildSuccess(pageResult);
    }

    /**
     * 根据ID查询部门
     */
    @Operation(summary = "根据ID查询部门", description = "根据ID查询部门")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "401", description = "未授权"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/getById/{id}")
    public Result<DepartmentVo> getById(@PathVariable Long id) {
        var departmentVo = departmentService.getById(id);
        return Result.buildSuccess(departmentVo);
    }

    /**
     * 修改 部门
     */
    @Operation(summary = "修改部门", description = "修改部门")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "401", description = "未授权"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping("/update")
    public Result<Boolean> update(@Valid @RequestBody DepartmentUpdateDto updateDto) {
        boolean flag = departmentService.update(updateDto);
        return Result.buildSuccess(flag);
    }

    /**
     * 新增部门
     * @param addDto
     * @return
     */
    @Operation(summary = "添加部门", description = "添加部门")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "401", description = "未授权"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping("/add")
    public Result<Boolean> add(@Valid @RequestBody DepartmentAddDto addDto) {

        boolean flag = departmentService.add(addDto);

        return Result.buildSuccess(flag);
    }
}